import uuid
from datetime import datetime
from enum import Enum
from typing import Optional, Any
from sqlalchemy import String, DateTime, JSON, func
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.dialects.postgresql import UUID
from app.core.database import Base

class DocumentStatus(str, Enum):
    PENDING = "Pending"         # 等待处理
    PROCESSING = "Processing"   # 正在解析/切片/Embedding
    INDEXED = "Indexed"         # 已存入向量库
    FAILED = "Failed"           # 处理失败

class Document(Base):
    __tablename__ = "documents"

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), 
        primary_key=True, 
        default=uuid.uuid4
    )
    filename: Mapped[str] = mapped_column(String(255), nullable=False)
    minio_object_name: Mapped[str] = mapped_column(String(1024), nullable=False)
    
    status: Mapped[DocumentStatus] = mapped_column(
        String(50), 
        default=DocumentStatus.PENDING,
        nullable=False
    )
    
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), 
        server_default=func.now()
    )
    
    # 存储文件大小、页数、解析配置等扩展信息
    metadata_: Mapped[Optional[dict[str, Any]]] = mapped_column("metadata", JSON, default={})

    def __repr__(self):
        return f"<Document id={self.id} filename={self.filename} status={self.status}>"